//
//  HNSH_RegisterViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_RegisterViewController.h"

//view
#import "HNSH_LoginInputView.h"

//model
#import "ST_TimeDownToors.h"

@interface HNSH_RegisterViewController ()
/* 手机号 */
@property (nonatomic, strong) HNSH_LoginInputView *phoneView;
/* 密码 */
@property (nonatomic, strong) HNSH_LoginInputView *pwdView;
/* 验证码 */
@property (nonatomic, strong) HNSH_LoginInputView *codeView;
/* 邀请码 */
@property (nonatomic, strong) HNSH_LoginInputView *invitatView;

/* 手机号 */
@property (nonatomic, copy) NSString *phoneString;
/* 密码 */
@property (nonatomic, copy) NSString *pwdString;
/* 验证码 */
@property (nonatomic, copy) NSString *codeString;
/* 邀请码 */
@property (nonatomic, copy) NSString *invitatString;
@end

@implementation HNSH_RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.phoneString = @"";
    self.pwdString = @"";
    self.codeString = @"";
    self.invitatString = @"";
    [self showNavTitle:@"" showLeftItem:YES];
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    [self configUI];
}

- (void)configUI {
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(32, kNavBarAndStatusBarHeight+70, 200, 28)];
    titleLabel.text = @"手机号注册";
    titleLabel.textColor = [UIColor appBlack333LabelColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [self.view addSubview:titleLabel];
    
    //手机号
    self.phoneView = [[HNSH_LoginInputView alloc]initWithFrame:CGRectMake(30, titleLabel.origin.y+titleLabel.height+46, kScreenWidth-60, 50) type:1];
    self.phoneView.placeholder = @"请输入手机号";
    [self.view addSubview:self.phoneView];
    
    //密码
    self.pwdView = [[HNSH_LoginInputView alloc]initWithFrame:CGRectMake(30, self.phoneView.origin.y+self.phoneView.height+16, kScreenWidth-60, 50) type:0];
    self.pwdView.placeholder = @"请输入密码";
    self.pwdView.hiddenText = YES;
    [self.view addSubview:self.pwdView];
    
    //验证码
    self.codeView = [[HNSH_LoginInputView alloc]initWithFrame:CGRectMake(30, self.pwdView.origin.y+self.pwdView.height+16, kScreenWidth-60, 50) type:2];
    self.codeView.placeholder = @"请输入验证码";
    [self.view addSubview:self.codeView];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:KUserShowHiddenContentID]) {
        //邀请码
        self.invitatView = [[HNSH_LoginInputView alloc]initWithFrame:CGRectMake(30, self.codeView.origin.y+self.codeView.height+16, kScreenWidth-60, 50) type:0];
        self.invitatView.placeholder = @"请输入邀请人手机号/邀请码（必填）";
        [self.view addSubview:self.invitatView];
    }
    
    //登录
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(30, self.codeView.origin.y+self.codeView.height+156, kScreenWidth-60, 50);
    [registerButton setTitle:@"注册" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
    registerButton.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    registerButton.backgroundColor = [UIColor appGreenColor];
    registerButton.layer.cornerRadius = 25.0f;
    registerButton.layer.masksToBounds = YES;
    [self.view addSubview:registerButton];
    
#pragma mark 操作区
    WeakSelf(weakSelf);
    //手机号
    self.phoneView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.phoneString = inputString;
    };
    //密码
    self.pwdView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.pwdString = inputString;
    };
    //验证码
    self.codeView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.codeString = inputString;
    };
    self.codeView.buttonBlock = ^(UIButton * _Nonnull sendButton) {
        [weakSelf loadSendCodeWithButton:sendButton];
    };
    //邀请码
    self.invitatView.fieldBlock = ^(NSString * _Nonnull inputString) {
        weakSelf.invitatString = inputString;
    };
    //注册
    [[registerButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf loadUserRegister];
    }];
}

#pragma mark networking
//发送验证码
- (void)loadSendCodeWithButton:(UIButton *)sendButton {
    if (![self.phoneString isNotBlank]) {
        [self.view makeToast:@"请输入手机号"];
        return;
    }
    [HNSH_UserNetServer requestSendCodeWithUserName:self.phoneString type:KSendCodeType_Register success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [[ST_TimeDownToors shareTimer] startTimerWithTime:60 complate:^(NSInteger time) {
                NSString *timeStr = [NSString stringWithFormat:@"%lds",time];
                [sendButton setTitle:timeStr forState:UIControlStateNormal];
                sendButton.enabled = NO;
                if (time == 0) {
                    [sendButton setTitle:@"重新发送" forState:UIControlStateNormal];
                    sendButton.enabled = YES;
                }
            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)loadUserRegister {
    if (![self.phoneString isNotBlank]) {
        [self.view makeToast:@"请输入手机号"];
        return;
    }
    if (![self.pwdString isNotBlank]) {
        [self.view makeToast:@"请输入密码"];
        return;
    }
    if (![self.codeString isNotBlank]) {
        [self.view makeToast:@"请输入验证码"];
        return;
    }
//    if (![self.invitatString isNotBlank]) {
//        [self.view makeToast:@"请输入邀请码"];
//        return;
//    }
    [HNSH_UserNetServer requestUserRegisterWithUserName:self.phoneString sms_code:self.codeString pwd:self.pwdString invite_code:self.invitatString success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            //保存token
            [[ST_UserManager shareInstance] saveAccountInfoWithAccessToken:responseObject.resultset[@"token"][@"access_token"] RefreshToken:responseObject.resultset[@"token"][@"refresh_token"]];
            //请求个人信息
            [[ST_UserManager shareInstance] loadUserInfoComplate:^{
                //隐藏判断
                KUserShowHiddenPhoneID
                kSTWindow.rootViewController = [ST_TabbarViewController new];
//                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            }];
            
            //请求融云信息
            [[ST_UserManager shareInstance] loadRcUserInfoComplate:^{
                [HNSH_RYTools connectWithToken:[ST_UserManager shareInstance].rcUser.token];
            }];
            
//            //请求三方信息
//            [[ST_UserManager shareInstance] loadOtherTokenComplate:^{
//                            
//            }];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
